import React from 'react'

import httpUtil from 'utils/request/index'
import { useHistory } from 'react-router-dom'
import './index.scss'
import { message, Modal, Spin, Button } from 'antd'
import img from '../../../../assets/images/pic_next.jpg'
import { tSCallSignatureDeclaration } from '@babel/types'

function Time(props) {
  const history = useHistory()
  //控制加载和显示页面
  const [loading, setLoading] = React.useState(false)
  const [isVisible, setIsVisible] = React.useState(true)

  const next = async () => {
    Modal.confirm({
      visible: isVisible,
      title: '是否推进下一周期？',
      okText: '确认',
      cancelText: '取消',
      async onOk() {
        setLoading(true)
        httpUtil.forwardCycle().then(res => {
          if (!res.status) {
            setTimeout(() => {
              setLoading(false)
            }, 500)
            switch (res.data.forwardStatus) {
              case 0:
                history.push('/gameover/checkJournalEntry')
                return message.error(res.data.message)
              case 1:
                history.push('/loading')
                history.push('/gameoperate/main')
                return message.success(res.data.message)
              case 2:
                message.error(res.data.message)
            }
          }
        })
      },
      onCancel() {
        setIsVisible(false)
      }
    })
  }

  const cancel = () => {
    setIsVisible(false)
  }

  const goNext = async () => {
    await setLoading(true)
    await setIsVisible(false)
    await httpUtil.forwardCycle().then(res => {
      console.log(res)
      if (!res.status) {
        switch (res.data.forwardStatus) {
          case 0:
            history.push('/gameover/checkJournalEntry')
            return message.error(res.data.message)
          case 1:
            history.push('/loading')
            setTimeout(() => {
              history.push('/gameoperate/main')
            }, 500)
            return message.success(res.data.message)
          case 2:
            message.error(res.data.message)
        }
      }
    })
    setLoading(false)
  }

  return (
    <div className='userprepare-wrapper'>
      {loading ? (
        <Spin size='large' />
      ) : (
        <img className='time-img' src={img} alt='进入下一周期' onClick={next} />
      )}
      {/* <Modal style={{ width: '200px' }} title='是否推进下一周期？' visible={isVisible} footer={null} onCancel={cancel}>
      <Button danger onClick={cancel}>取消</Button>
      <Button type='primary' onClick={goNext}>确认</Button>
    </Modal> */}
    </div>
  )
}

export default Time
